
#include <iostream>
#include <string>
#include <sstream>
#include "SDL/SDL_net.h"

using namespace std;

#undef main
int main(int argc, char **argv)
{
	int type;
	cout<<"server 0: "<<endl;
	cin>>type;
	if(type==0){
		UDPsocket sd;       /* Socket descriptor */
		UDPpacket *p;       /* Pointer to packet memory */
		int quit;

		/* Initialize SDL_net */
		if (SDLNet_Init() < 0)
		{
			fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		/* Open a socket */
		if (!(sd = SDLNet_UDP_Open(2000)))
		{
			fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		/* Make space for the packet */
		if (!(p = SDLNet_AllocPacket(512)))
		{
			fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		cout << "Server created" << endl;

		/* Main loop */
		quit = 0;
		while (!quit)
		{
			SDL_Delay(100);
			/* Wait a packet. UDP_Recv returns != 0 if a packet is coming */
			while (SDLNet_UDP_Recv(sd, p))
			{
				/*cout<<"UDP Packet incoming"<<endl;
				cout<<"\tChan:    "<<p->channel<<endl;
				cout<<"\tData:    "<< (char *)p->data<<endl;
				cout<<"\tLen:     "<< p->len<<endl;
				cout<<"\tMaxlen:  "<< p->maxlen<<endl;
				cout<<"\tStatus:  "<< p->status<<endl;
				cout<<"\tAddress: "<< p->address.host<<" "<< p->address.port<<endl;
				*/
				cout<<"\tData:    "<< (char *)p->data << endl;
				/* Quit if packet contains "quit" */

			}
		}

		/* Clean and exit */
		SDLNet_FreePacket(p);
		SDLNet_Quit();
		return EXIT_SUCCESS;
	}else{


		UDPsocket sd;
		IPaddress srvadd;
		UDPpacket *p;
		int quit;

		/* Check for parameters */
		if (argc < 3)
		{
			fprintf(stderr, "Usage: %s host port\n", argv[0]);
			exit(EXIT_FAILURE);
		}

		/* Initialize SDL_net */
		if (SDLNet_Init() < 0)
		{
			fprintf(stderr, "SDLNet_Init: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		/* Open a socket on random port */
		if (!(sd = SDLNet_UDP_Open(0)))
		{
			fprintf(stderr, "SDLNet_UDP_Open: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		/* Resolve server name  */

		if (SDLNet_ResolveHost(&srvadd, argv[1], atoi(argv[2])) == -1)
		{
			fprintf(stderr, "SDLNet_ResolveHost(%s %d): %s\n", argv[1], atoi(argv[2]), SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		/* Allocate memory for the packet */
		if (!(p = SDLNet_AllocPacket(512)))
		{
			fprintf(stderr, "SDLNet_AllocPacket: %s\n", SDLNet_GetError());
			exit(EXIT_FAILURE);
		}

		cout << "Connection established on " << argv[1] << endl;

		/* Main loop */
		quit = 0;

		const char *h;
		int current=0;
		while (!quit)
		{
			SDL_Delay(10);
			current++;
			//cout << "Fill the buffer" << endl << ">";
			cin >> (char *)p->data;
			//stringstream ss;
			//ss << current;
			//h = ss.str().c_str();
			//p->data = (Uint8*)h;
			p->address.host = srvadd.host;	/* Set the destination host */
			p->address.port = srvadd.port;	/* And destination port */

			p->len = strlen((char *)p->data) + 1;
			SDLNet_UDP_Send(sd, -1, p); /* This sets the p->channel */

			/* Quit if packet contains "quit" */
			//if (!strcmp((string)p->data, "quit"))
			//	quit = 1;
		}

		SDLNet_FreePacket(p);
		SDLNet_Quit();

		return EXIT_SUCCESS;
	}
}
